package com.minyifang.wcspf.work.ui.goods.presenter;

import com.minyifang.wcspf.common.base.BasePresenter;
import com.minyifang.wcspf.common.config.network.Constants;
import com.minyifang.wcspf.common.config.network.RetrofitHelper;
import com.minyifang.wcspf.common.config.user.UserManager;
import com.minyifang.wcspf.work.ui.goods.view.GoodsDetailsView;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class GoodsDetailsPresenter extends BasePresenter<GoodsDetailsView> {
    /**
     * 获取商品详情
     */
    public void getGoodsDetails(String id) {
        Map<String, Object> data = new HashMap<>();
        data.put("comId", id);
        final JSONObject jsonData = Constants.getJsonData(data, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().getGoodsDetails(requestBody), goodsDetailBeanHttpRespond -> {
            if (goodsDetailBeanHttpRespond.result == 1) {
                getView().onGetGoodsDetailsSucceed(goodsDetailBeanHttpRespond.data);
            }
        });
    }

    public void collectionGoods(String goodsId, int type) {
        Map<String, Object> data = new HashMap<>();
        data.put("comId", goodsId);
        data.put("type", type);
        JSONObject aesJsonData = Constants.getAESJsonData(data, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        getView().showLoadingView();
        addTask(RetrofitHelper.getInstance().getService().collectionGoods(requestBody), respond -> {
            getView().hideLoadingView();
            getView().showCollectionRes(respond);
        });
    }
}
